package gui;

import gui.Main.GUI;
import hypeerweb.BroadcastVisitor;
import hypeerweb.Node;
import hypeerweb.Parameters;

/**
 * Broadcasts a message from the start node (the first node to receive this visitor) to all other nodes in the HyPeerWeb. Every time the message reaches node the indicated node and message are printed
 * in the "TraceInformation" section of the GUI.
 * 
 * <pre>
 * 		<b>Domain:</b> <i>None</i>
 * </pre>
 */
public class Broadcaster extends BroadcastVisitor {
	/**
	 * The default constructor. It does nothing but call the superclass's default constructor.
	 * 
	 * @pre <i>None</i>
	 * @post super.post-condition
	 */
	public Broadcaster() {}

	/**
	 * Creates the parameters needed to send the Broadcaster visitor to the first Node.
	 * 
	 * @param message
	 *            the message to be broadcast to all node.
	 * @pre <i>None</i>
	 * @post result &ne; null AND result.contains(MESSAGE_KEY) AND result.get(MESSAGE_KEY) = message
	 */
	public static Parameters createInitialParameters(String message, GUI main) {
		Parameters p = new Parameters();
		p.set(MESSAGE_KEY, message);
		p.set(GUI_KEY, main);
		p.set(STARTED_KEY, false);
		return p;
	}

	@Override
	/**
	 * Prints a string in the TracePanel of the GUI.  The string should contain the message and the
	 * labeled webId of the current node.
	 * 
	 * @pre node &ne; null AND node &isin; HyPeerWeb AND parameters &ne; null AND parameters.contains(MESSAGE_KEY)
	 * @post A string with the message and current node's id should be printed on the tracePanel of the GUI.<br>
	 * Required format: "Broadcasting '" parameters.get(MESSAGE_ID) "' to node " node.getWebId() ".\n"
	 */
	protected void operation(Node node, Parameters parameters) {
		GUI main = (GUI) parameters.get(GUI_KEY);
		String message = (String) parameters.get(MESSAGE_KEY);
		main.printToTracePanel("Broadcasting " + message + " to node " + node.getWebId().getValue() + ".\n");
	}

	/**
	 * The message parameter identifier to be used to add messages to the parameter list.
	 */
	private static final String MESSAGE_KEY = "message";

	private static final String GUI_KEY = "gui";
}
